﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SouthApps.MobileHealth.SDK.DAL.DataAccess;
using SouthApps.MobileHealth.SDK.Common.Exceptions;
using SouthApps.MobileHealth.SDK.DAL.Model;
using SouthApps.MobileHealth.SDK.Common;

namespace SouthApps.MobileHealth.SDK.DAL.Repositories
{
    public class SecurityRepository : ISecurityRepository
    {
        UserDao userDao;
        PermissionDao permissionDao;

        public SecurityRepository()
        {
            this.userDao = new UserDao();
            this.permissionDao = new PermissionDao();
        }

        public User GetUser(string username, string password, int financiadorId)
        {
            try
            {
                return this.userDao.GetUser(username, password, financiadorId);
            }
            catch (SystemException ex)
            {
                throw new DataAccessException(DataAccessErrorCode.UnknownError, "A database error ocurred while getting user: {0}.", ex.Message);
            }
        }

        public List<Permission> GetUserPermissions(int userId)
        {
            try
            {
                return this.permissionDao.GetUserPermissions(userId);
            }
            catch (SystemException ex)
            {
                throw new DataAccessException(DataAccessErrorCode.UnknownError, "A database error ocurred while getting user permissions: {0}.", ex.Message);
            }
        }

        public void UpdateUserLoginData(int userId, DateTime? lastLoginDate, bool loggedIn)
        {
            try
            {
                this.userDao.UpdateUserLoginData(userId, lastLoginDate, loggedIn);
            }
            catch (SystemException ex)
            {
                throw new DataAccessException(DataAccessErrorCode.UnknownError, "A database error ocurred while getting user permissions: {0}.", ex.Message);
            }
        }
    }
}
